#include "MJUI.h"
#include "ui_MJUI.h"
#include <QPainter>
#include <QMessageBox>

MJUI::MJUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MJUI)
{

    const float TIMER_TIMEOUT = 16 ;
    ui->setupUi(this);

    MJStone::init();

    selectTopMJ = -1;
    selectHandMJ = -1;

    for(auto i:MJStone::allKindStone){
        drawAllStone.push_back(MJStone::getPicMapByNum(i));
    }
    this->startTimer(TIMER_TIMEOUT);

}
void MJUI::timerEvent(QTimerEvent *event)
{
    update();
}



MJUI::~MJUI()
{
    delete ui;
}



void MJUI::paintEvent(QPaintEvent *event){
    QPainter painter(this);

    QRect DisCardStone(0, 356, 732, 104);             //弃牌
    painter.fillRect(DisCardStone, QColor(255, 0, 0));
    painter.drawText(0,356, "弃牌区");
    QRect SuggestDisCardStone(600, 356, 732, 104);             //建议放弃手牌区
    painter.fillRect(SuggestDisCardStone, QColor(255, 255, 0));
    painter.drawText(600,356, "帮助区");

    QRect HandStone(0, 460, 732, 104);                  //手牌区
    painter.fillRect(HandStone, QColor(0, 255, 0));
    painter.drawText(0,460, "手牌区");

    QRect WaitStone(0, 564, 732, 100);                    //听牌区
    painter.fillRect(WaitStone, QColor(0, 0, 255));
    painter.fillRect(HandStone, QColor(0, 255, 0));
    painter.drawText(0,564, "听牌区");

    for(int j = 0;j < 4 ;j++){
        for(int i = 0;i < 9&& j*9+i < 34;i++){
            painter.drawPixmap( (i*78) + 30,20 + j * 84, 48, 64,drawAllStone[j*9+i]);
            int num = MJStone::allStone[MJStone::allKindStone[j*9+i]];
            QString numstr = QString::number(num,10);
            painter.drawText((i*78) + 30,20 + j * 84, numstr);
        }
    }

    if(selectTopMJ != -1){
        painter.drawPixmap(tempPosition.x(),tempPosition.y(),48,64,
                           MJStone::getPicMapByNum(MJStone::allKindStone[selectTopMJ]));
    }
    if(selectHandMJ != -1){
        painter.drawPixmap(tempPosition.x(),tempPosition.y(),48,64,
                           MJStone::getPicMapByNum(MJStone::handStoneVec[selectHandMJ]));
    }
    sort(MJStone::handStoneVec.begin(),MJStone::handStoneVec.end());
    for(int i = 0;i < MJStone::handStoneVec.size();i++){
        painter.drawPixmap(i*48, 480, 48, 64,MJStone::getPicMapByNum(MJStone::handStoneVec[i]));
    }

    for(int i = 0;i < MJStone::waitStoneVec.size();i++){
        painter.drawPixmap(i*48,584,MJStone::getPicMapByNum(MJStone::waitStoneVec[i]));
    }


}
/**
 * @brief MJ::mousePressEvent : 鼠标点击事件，取牌操作
 * @param topPai == 取牌顶
 *        handPai == 取手牌
 */
void MJUI::mousePressEvent(QMouseEvent *event){

    clickPosition = event->pos();
    tempPosition = clickPosition;

    if(0< clickPosition.y()&& clickPosition.y() < 356){ //在牌顶区
        int x = clickPosition.x()%78;
        int i = clickPosition.x()/78;//横向
        int y = clickPosition.y()%84;
        int j = clickPosition.y()/84;//纵向
        if(30 > x||20 > y){
            selectTopMJ = -1;
            update();
            return;
        }
        selectTopMJ = j * 9 + i;
        selectHandMJ = -1;
    }
    if(480 < clickPosition.y()&&clickPosition.y() < 544){ //在手牌区
        if(clickPosition.x() < MJStone::handStoneVec.size()*48){
            int b = clickPosition.x()/48;
            selectTopMJ = -1;
            selectHandMJ = b;
            update();
        }

    }

}

void MJUI::mouseMoveEvent(QMouseEvent *event){
    tempPosition = event->pos();
    update();
}

void MJUI::mouseReleaseEvent(QMouseEvent *event) {

    releasePosition = event->pos();

    if(0< releasePosition.y()&& releasePosition.y() < 356){
        update();
        return;
    }
    if(356< releasePosition.y()&& releasePosition.y() < 460){//这里是弃牌
        if(selectHandMJ != -1){

            int Ret = QMessageBox::information(NULL, "MJMaphack", "你确定要出这张牌吗", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if(Ret == QMessageBox::Yes){
                for(int i = selectHandMJ;i < MJStone::handStoneVec.size();i++){
                    MJStone::handStoneVec[i] = MJStone::handStoneVec[i+1];
                }
                MJStone::handStoneVec.pop_back();
                update();
            }
            if(Ret == QMessageBox::No){
                selectHandMJ = -1;
            }
        }
        if(selectTopMJ!=-1){                                 //弃牌的时候要将牌堆中的内容踢掉
            int Ret = QMessageBox::information(NULL, "MJMaphack", "你确定别人出了这张牌吗", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if(Ret == QMessageBox::Yes){
                if(MJStone::allStone[MJStone::allKindStone[selectTopMJ]] > 0)
                MJStone::allStone[MJStone::allKindStone[selectTopMJ]]--;
            }
            if(Ret== QMessageBox::No){
                selectTopMJ = -1;
                update();
            }
        }
    }
    if(460 < releasePosition.y()&&releasePosition.y() < 564){
        if(selectTopMJ!=-1){
            MJStone::waitStoneVec.clear();
//            int Ret = QMessageBox::information(NULL, "MJMaphack", "你确定要取这张牌吗", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
//            if(Ret == QMessageBox::Yes){
                if(MJStone::allStone[MJStone::allKindStone[selectTopMJ]] > 0 && MJStone::handStoneVec.size() <14){
                    MJStone::allStone[MJStone::allKindStone[selectTopMJ]]--;
                    MJStone::handStoneVec.push_back(MJStone::allKindStone[selectTopMJ]);
                }
                if(MJStone::handStoneVec.size() == 14&&MJStone::win(MJStone::handStoneVec)){
                    selectTopMJ = -1;
                    update();

                    QMessageBox::information(NULL,"恭喜","你胡牌了");
                    update();
                    return;
                }else if(MJStone::handStoneVec.size() == 14 &&MJStone::discard(MJStone::handStoneVec)) {
                    update();
                    QMessageBox::information(NULL,"听牌可胡","建议放弃手牌" + MJStone::stoneToName(MJStone::disCard));
                    selectHandMJ = -1;
                    update();
                    return;
                }
//            }
//            if(Ret == QMessageBox::No){
//                selectTopMJ = -1;
//                update();
//            }

        }
    }
    selectTopMJ = -1;
    selectHandMJ = -1;
    update();
}

void MJUI::keyPressEvent(QKeyEvent *event){
    if (event->key() == Qt::Key_Z&& (event->modifiers() & Qt::ControlModifier)){//撤销操作

    }
    if(event->key() == Qt::Key_Y&& (event->modifiers() & Qt::ControlModifier)){//反撤销操作

    }
}
//QMessageBox::information(NULL, "MJMaphack", helpMessage(), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
